package work4.tcp;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        //创建socket对象
        try {



            Socket socket = new Socket();
            SocketAddress socAddress = new InetSocketAddress("127.0.0.1", 2021);

            socket.connect(socAddress,10 * 1000);
            System.out.println("服务器连接成功");
            Scanner scanner = new Scanner(System.in);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            while (true){
                System.out.println("请输入账号：");

                String account = scanner.next();
                System.out.println("请输入密码:");
                String password = scanner.next();
                UserMessage message = new UserMessage("login",new UserMessage.User(account,password));

                objectOutputStream.writeObject(message);

                UserMessage getMsg = (UserMessage) objectInputStream.readObject();
                if (getMsg.getStatus().equals("success")){
                    System.out.println("登录成功");
                    objectInputStream.close();
                    objectOutputStream.close();
                    return;
                } else {
                    System.out.println("登录失败");
                }
            }


        } catch (IOException | ClassNotFoundException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

    }
}
